package com.zhiwang.planet.mvp.p

import com.zhiwang.planet.di.ActivityScope
import com.zhiwang.planet.mvp.v.AccountBindView
import com.zhiwang.planet.mvp.v.SmsCodeView
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.mvvm.model.entity.UserDao
import com.zhiwang.planet.mvvm.model.request.BindAliAccountRequest
import com.zhiwang.planet.mvvm.model.request.SmsCodeRequest
import com.zhiwang.planet.util.androidSchedule
import com.zhiwang.planet.util.responseVoidFunc
import javax.inject.Inject

@ActivityScope
class BindPresenter @Inject constructor(var restApi: RestApi,var userDao: UserDao) {


    lateinit var smsCodeView: SmsCodeView

    lateinit var bindAccountView: AccountBindView

    fun bindAliAccount(aliAccount: String, realName: String, graphCode: String,smsCode:String) {

        val bindAliAccountRequest = BindAliAccountRequest(aliAccount, realName, graphCode,smsCode)
        var subscribe = restApi
                .bindAliAccount(bindAliAccountRequest)
                .androidSchedule()
                .responseVoidFunc()
                .subscribe({

                }, {
                    bindAccountView.onError(it)
                }, {
                    val user = userDao.getFirstUser()
                    user.bindInfo.aliAccountName = aliAccount
                    user.bindInfo.aliRealName = realName
                    user.bindInfo.isAliBind = true
                    userDao.updateUser(user)

                    bindAccountView.onAccountBind()

                })


    }

    fun getBindSmsCode(phone: String, seed: String, graphCode: String) {
        val request = SmsCodeRequest(phone = phone, graphCode = graphCode, randomStr = seed)
        request.setTypeBindNoPic()
        var subscribe = restApi.getSmsCode(request)
                .androidSchedule()
                .responseVoidFunc()
                .subscribe({

                }, {
                    smsCodeView.onError(it)
                }, {
                    smsCodeView.onSmsSend()
                })
    }

    fun getBindPayPassWordSmsCode(phone: String, seed: String, graphCode: String) {
        val request = SmsCodeRequest(phone = phone, graphCode = graphCode, randomStr = seed)
        request.setTypeBind()
        var subscribe = restApi.getSmsCode(request)
                .androidSchedule()
                .responseVoidFunc()
                .subscribe({

                }, {
                    smsCodeView.onError(it)
                }, {
                    smsCodeView.onSmsSend()
                })
    }

}